<template>
  <page-header-wrapper :title="false">
    <a-card>
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="6" :sm="24">
              <a-form-item label="筛选">
                <a-input :value="value" placeholder="请输入" />
              </a-form-item>
            </a-col>
            <a-col :md="(!advanced && 8) || 24" :sm="24">
              <span
                class="table-page-search-submitButtons"
                :style="(advanced && { float: 'right', overflow: 'hidden' }) || {}"
              >
                <a-button type="primary" @click="handleSearch(true)" :loading="loading">查询</a-button>
                <a-button style="margin-left: 8px" @click="() => (queryParam = {})">重置</a-button>
                <!-- <a @click="toggleAdvanced" style="margin-left: 8px">
                  {{ advanced ? '收起' : '展开' }}
                  <a-icon :type="advanced ? 'up' : 'down'" />
                </a> -->
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
      <div class="table-operator">
        <a-button type="primary" icon="plus" @click="handleChange()">手动变更</a-button>
      </div>

      <a-table ref="table" size="default" rowKey="key" :columns="columns" :data-source="loadData">
        <span slot="serial" slot-scope="text, record, index">
          {{ index + 1 }}
        </span>
        <!-- <span slot="status" slot-scope="text, record">
          <template>
            <a-tag color="#82af6f" v-if="record?.status == 1">启用中</a-tag>
            <a-tag color="#abbac3" v-else>已停用</a-tag>
          </template>
        </span> -->
        <span slot="action" slot-scope="text, record">
          <template>
            <a-tag color="#D15B47" @click="handleDelete(record)" v-if="record.chgType == '1'">删除</a-tag>
            <a-tag color="#428bca" @click="handleDetail(record)" v-if="record.chgType == '0'">进入</a-tag>
          </template>
        </span>
      </a-table>
    </a-card>
    <create-form
      ref="createModal"
      :visible="visible"
      :loading="confirmLoading"
      :model="mdl"
      @cancel="handleCancel"
      @ok="handleOk"
    />
  </page-header-wrapper>
</template>

<script>
import { getMemberIntegralListUsingPOST ,deleteIntegralUsingPOST,updateIntegralUsingPOST} from '@/api/api/huiyuanguanli.ts'
import CreateForm from './CreateForm'
export default {
  name: 'MemberListDetialIntegral',
  components: {
    CreateForm,
  },
  data() {
    return {
      mdl: null,
      priceMdl: null,
      visible: false,
      priceVisible: false,
      value: '',
      confirmLoading: false,
      priceLoading: false,
      // 高级搜索 展开/关闭
      advanced: false,
      // 查询参数
      queryParam: {
        pageNo: 1,
        pageSize: 10,
        queryCondition: '',
        status: '',
      },
      // 表头
      columns: [
        {
          title: '记录日期',
          dataIndex: 'createTime',
        },
        {
          title: '积分变化',
          dataIndex: 'chgValue',
        },
        {
          title: '记录原因',
          dataIndex: 'memo',
        },
        {
          title: '操作',
          dataIndex: 'action',
          width: '180px',
          scopedSlots: { customRender: 'action' },
        },
      ],
      loading: false,
      // 加载数据方法 必须为 Promise 对象
      loadData: [],
      optionAlertShow: false,
    }
  },
  created() {
    this.handleSearch(this.queryParam)
  },
  methods: {
    handleDelete (record) {
      const that = this
      this.$confirm({
        title: '删除',
        content: '确定要撤销这条变更积分了吗？',
        okText: '确认',
        okType: 'danger',
        cancelText: '取消',
        onOk() {
          deleteIntegralUsingPOST({ id: record.id }).then(() => { 
            that.handleSearch(that.queryParam)
            that.$message.success('操作成功')
            })
            .catch((error) => {
              that.$message.error(((error.response || {}).data || {}).msg || '请求出现错误')
            })
        },
      })
    },
    handleDetail(record) {
      this.$router.push({ path: `/cashier/profile/index/${record.orderId}` })
    },
    handleSearch() {
      this.loading = true
      getMemberIntegralListUsingPOST({...this.queryParam,memId:this.$route.params.id})
        .then((res) => {
          this.loading = false
          this.loadData = res.data.list
        })
        .catch((error) => {
          this.loading = false
          this.$message.error(((error.response || {}).data || {}).msg || '请求出现错误')
        })
    },
    handleChange() {
      this.mdl = null
      this.visible = true
    },
    handleOk() {
      const form = this.$refs.createModal.form
      this.confirmLoading = true
      form.validateFields((errors, values) => {
        if (!errors) {
          updateIntegralUsingPOST({ ...values,memId:this.$route.params.id })
            .then(() => {
              this.handleSearch(this.queryParam)
              this.$message.success('操作成功')
              this.visible = false
              this.confirmLoading = false
              // 重置表单数据
              form.resetFields()
            })
            .catch((error) => {
              this.$message.error(((error.response || {}).data || {}).msg || '请求出现错误')
            })
        } else {
          this.confirmLoading = false
        }
      })
    },
    handleCancel() {
      this.visible = false
      const form = this.$refs.createModal.form
      form.resetFields() // 清理表单数据（可不做）
    },

    toggleAdvanced() {
      this.advanced = !this.advanced
    },
  },
}
</script>
